#include <stdio.h>
#include <microhttpd.h>
#include <string.h>

#include "httpd.h"
#include "jerperter.h"

int accesshandler(void *cls, struct MHD_Connection * connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
{
    static int dummy;
    char *page ="{ \"result\" = \"ok\"}";
    
    if(&dummy != *con_cls)
    {
        printf("first call\n");
        *con_cls = &dummy;
        return MHD_YES;
    }
    printf("second reply\n");
    if(*upload_data_size) 
        if(main_interperter((char *)upload_data, (char *)url))
        {
            *upload_data_size = 0;
            return MHD_YES;
        }
    struct MHD_Response *res = MHD_create_response_from_buffer(strlen(page), page, MHD_RESPMEM_PERSISTENT);
    MHD_queue_response(connection, 404, res);
    MHD_destroy_response(res);
    return MHD_YES;
}